package com.wxtwxt.ruralmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wxtwxt.ruralmall.common.utils.R;
import com.wxtwxt.ruralmall.product.entity.ProductAttrValueEntity;
import com.wxtwxt.ruralmall.product.service.ProductAttrValueService;
import com.wxtwxt.ruralmall.product.vo.AttrResponseVo;
import com.wxtwxt.ruralmall.product.vo.AttrVo;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.*;

import com.wxtwxt.ruralmall.product.entity.AttrEntity;
import com.wxtwxt.ruralmall.product.service.AttrService;
import com.wxtwxt.ruralmall.common.utils.PageUtils;

import javax.annotation.Resource;



/**
 * 商品属性
 *
 * @author Wxt
 * @email wxt23787@126.com
 * @date 2023-01-30 19:53:31
 */
@EnableTransactionManagement
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Resource
    private AttrService attrService;
    @Resource
    private ProductAttrValueService productAttrValueService;

    @PostMapping("/update/{spuId}")
    public R updateBaseForSku(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> list) {
        productAttrValueService.updateBaseForSku(spuId, list);
        return R.ok();
    }

    @GetMapping("/base/listforspu/{spuId}")
    public R getBaseListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.getBaseListForSpu(spuId);
        return R.ok().put("data", productAttrValueEntityList);
    }

    @GetMapping("/{type}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId, @PathVariable("type") String type) {
        PageUtils page = attrService.queryBaseAttrList(params, catelogId, type);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrResponseVo attrResponseVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrResponseVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttrVo(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
